package com.future.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.future.dao.AreaDAO;
import com.future.dao.DepartmentDAO;
import com.future.dao.InformationDAO;
import com.future.dao.ServicesDAO;
import com.future.dao.ShareDepartmentDAO;
import com.future.dao.UserDAO;
import com.future.model.po.AreaModel;
import com.future.model.po.DepartmentModel;
import com.future.model.po.InformationModel;
import com.future.model.po.ServicesModel;
import com.future.model.po.ShareDepartmentModel;
import com.future.model.po.UserModel;

/**
 * 初始化字典表中数据的工具类 使用前需将待导入数据放到classes/dataimport目录下，然后执行
 */
public class ImportData {
    private static Logger logger = LogManager.getLogger(ImportData.class);

    public static void main(String[] args) {
        ImportData importData = new ImportData();
        // 导入服务事项
        // importData.importServices();
        // 导入34部门 (包含地方 、街乡、及 32部门)
//		 importData.importDepartments();
        // 导入信息共享部门
//		importData.importShareDepartment();
        // 导入信息共享内容
//		importData.importInformation();
        // 导入行政区划
        importData.importArea();
        // 导入用户信息
//		 importData.importUser();
    }

    public void importDepartments() {
        List<String> lines = this.readFile("department.txt");
        Iterator<String> iter = lines.iterator();
        DepartmentDAO dao = new DepartmentDAO();

        DepartmentModel model = new DepartmentModel();

        while (iter.hasNext()) {
            model.setDepartment(iter.next());
            dao.saveDepartment(model);
        }

    }

    public void importServices() {
        List<String> lines = this.readFile("services.txt");
        Iterator<String> iter = lines.iterator();

        ServicesDAO dao = new ServicesDAO();
        ServicesModel service = new ServicesModel();

        while (iter.hasNext()) {
            String s = iter.next();
            service.setServices(s);
            dao.saveServices(service);
        }
    }

    public void importShareDepartment() {
        List<String> lines = this.readFile("sharedepartment.txt");
        Iterator<String> iter = lines.iterator();

        ShareDepartmentDAO dao = new ShareDepartmentDAO();
        ShareDepartmentModel model = new ShareDepartmentModel();

        while (iter.hasNext()) {
            String s = iter.next();
            model.setDepartment(s);
            dao.saveShareDepartment(model);
        }
    }

    public void importArea() {
//		List<String> lines = this.readFile("area_sample.txt");
        List<String> lines = this.readFile("street.txt");
        Iterator<String> iter = lines.iterator();
        AreaModel model;
        AreaDAO dao = new AreaDAO();
        while (iter.hasNext()) {
            model = new AreaModel();
            String line = iter.next();
            String[] param = line.split(",");
            model.setArea(param[1]);
            model.setCode(param[0]);
            dao.save(model);
        }
    }

    public void importUser() {
        List<String> lines = this.readFile("user_sample.txt");
        Iterator<String> iter = lines.iterator();
        UserModel user = null;
        UserDAO dao = new UserDAO();
        while (iter.hasNext()) {
            String[] params = iter.next().split(",");
            user = new UserModel();
            user.setUsername(params[0]);
            user.setPassword(params[1]);
            user.setDepartmentId(Integer.valueOf(params[2]));
            user.setAreaId(Integer.valueOf(params[3]));
            dao.saveUser(user);
        }
    }

    public void importInformation() {
        List<String> lines = this.readFile("information.txt");
        Iterator<String> iter = lines.iterator();
        InformationModel model = new InformationModel();
        InformationDAO dao = new InformationDAO();
        while (iter.hasNext()) {
            String line = iter.next();
            String[] param = line.split(",");
            model.setDeptid(Integer.valueOf(param[0]));
            model.setInformation(param[1]);
            dao.save(model);
        }
    }

    private List<String> readFile(String fileName) {
        String path = ClassLoader.getSystemResource("").toString().substring(6) + "dataimport/";
        InputStream fis;
        BufferedReader br;
        String line;
        List<String> textLines = new ArrayList<String>();
        try {
            fis = new FileInputStream(new File(path + fileName));
            br = new BufferedReader(new InputStreamReader(fis, Charset.forName("UTF-8")));
            while ((line = br.readLine()) != null) {
                textLines.add(line);
            }
        } catch (FileNotFoundException e) {
            logger.error(e);
        } catch (IOException e) {
            logger.error(e);
        }
        return textLines;
    }

}
